(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.resourceBundle.createRejectController', createRejectController);
	createRejectController.$inject = ['$q', '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'resourceBundleService' ];
	function createRejectController($q, $scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, resourceBundleService) {
		var ctrl = this, resourceBundleInfo = $scope.$stateParams.params, errorMessage = {}, copyDatas = [];
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.resourceBundle = {};

		ctrl.init = function() {
			ctrl.resourceBundle = resourceBundleInfo;
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, null, sortChanged);
			ctrl.gridOptions.enableRowHeaderSelection = false;
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			resourceBundleInfo.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			authService.createReject(resourceBundleInfo).then(function(response) {
				if (response.data && response.data.model && response.data.task) {
					ctrl.resourceBundle = response.data.model;
					angular.merge(ctrl.searchCriteria, response.data.model.searchCriteria);
					gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
					ctrl.resourceBundle.taskId = response.data.task.taskId;
				}
			});
			authService.initialOption().then(function(response) {
				ctrl.localOptions = response.data.localList;
				ctrl.refBundleOptions = response.data.bundleList;
			});
			resourceBundleService.getResourceBundleScopeOptions().then(function(response) {
				ctrl.scopeOptions = response;
			});
		};
		
		//copy data
		function copyResMsgDatas(){
			if(ctrl.searchCriteria && ctrl.searchCriteria.list){
				for(var i=0; i < ctrl.searchCriteria.list.length; i++){
					if(ctrl.searchCriteria.list[i].resMsg != null && ctrl.searchCriteria.list[i].resMsg != ""){
						if(copyDatas.length > 0){
							var counts = "";
							var ms = "";
							angular.forEach(copyDatas, function(data,
									index, array) {
								var dat ="."+data.resKeyId;
								ms = ms + dat;
							});
							counts = ms+".";
							var id ="."+ctrl.searchCriteria.list[i].resKeyId+".";
							if (counts.indexOf(id) != -1) {
								for(var n=0; n < copyDatas.length; n++){
									if(copyDatas[n].resKeyId == ctrl.searchCriteria.list[i].resKeyId){
										if(copyDatas[n].resMsg != ctrl.searchCriteria.list[i].resMsg){
											copyDatas[n].resMsg = ctrl.searchCriteria.list[i].resMsg;
										}
									}
								}
							}else{
								copyDatas.push(ctrl.searchCriteria.list[i]);
							}
						}else{
						    copyDatas.push(ctrl.searchCriteria.list[i]);
						}
					}
				}
			};
		}
		
		//set data
		function setResMsgDatas(){
			if(ctrl.searchCriteria && ctrl.searchCriteria.list && copyDatas.length > 0){
			    for(var j = 0; j < copyDatas.length; j++){
			    	for(var k = 0; k < ctrl.searchCriteria.list.length; k++){
			    		if(copyDatas[j].resKeyId == ctrl.searchCriteria.list[k].resKeyId){
			    			ctrl.searchCriteria.list[k].resMsg = copyDatas[j].resMsg;
			    		}
			    	}
			    }
			}
		}
		
		ctrl.search = function() {
			var defer = $q.defer();
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(
			function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
				defer.resolve({});
			},angular.noop);
			return defer.promise;
		};

		function paginationChanged(pageNum, pageSize) {
			//copy data
			copyResMsgDatas();
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search().then(function(response){
				//set data
				setResMsgDatas();
			});
		}

		ctrl.createRejectSave = function() {
			if(copyDatas.length > 0){
				ctrl.resourceBundle.resources = copyDatas;
			}else{
				ctrl.resourceBundle.resources = ctrl.searchCriteria.list;
			}
			return authService.createRejectSave(ctrl.resourceBundle).then(saveSuccessful, saveFailed);
		};

		ctrl.createRejectSubmit = function() {
			if(copyDatas.length > 0){
				ctrl.resourceBundle.resources = copyDatas;
			}else{
				ctrl.resourceBundle.resources = ctrl.searchCriteria.list;
			}
			return authService.createRejectSubmit(ctrl.resourceBundle).then(submitSuccessful, submitFailed);
		};
		
		ctrl.createRejectCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.resourceBundle.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createRejectCancel(ctrl.resourceBundle).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.resourcebundle.list',
					'state' : 'cda.resourceBundle.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				angular.extend(ctrl.resourceBundle, response.data.model);
			}else if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.resourcebundle.list',
					'state' : 'cda.resourceBundle.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			copyResMsgDatas();
			ctrl.search().then(function(response){
				setResMsgDatas();
			});
		}
	}
})(angular);